package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import liumengbaio.lolzhangyoubao.Activity.Adapter.MoreNovelDetailsAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.MoreNovelDetailsInfo;
import liumengbaio.lolzhangyoubao.Activity.InFo.MoreNovelInfo;
import liumengbaio.lolzhangyoubao.R;

/**
 * 小说详情页面
 * Created by lanouhn on 16/9/5.
 */
public class MoreNovelDetailsActivity extends AppCompatActivity {

    private TextView title, list, content;
    private ImageView download, back, icon;
    private MoreNovelDetailsInfo mMoreNovelDetailsInfo;
    private MoreNovelDetailsAdapter mMoreNovelDetailsAdapter;
    private RecyclerView rl;
    private String id;
    private RelativeLayout rl_head;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        initView();
        initData();
        initEvent();
        getDataFromWeb();
    }

    private void initView() {
        title = (TextView) findViewById(R.id.video_activity_tvTitle);
        list = (TextView) findViewById(R.id.video_list);
        content = (TextView) findViewById(R.id.video_activity_tvContent);
        back = (ImageView) findViewById(R.id.video_activity_back);
        download = (ImageView) findViewById(R.id.video_activity_download);
        icon = (ImageView) findViewById(R.id.video_activity_ivHead);
        rl = (RecyclerView) findViewById(R.id.video_activity_rv);
        rl_head = (RelativeLayout) findViewById(R.id.video_activity_rl);
    }

    private void initData() {
        title.setText("小说详情");
        download.setVisibility(View.INVISIBLE);
        list.setText("小说章节列表");
        icon.setScaleType(ImageView.ScaleType.CENTER_CROP);

        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        title.setText(name);
        final String desc = intent.getStringExtra("desc");
        content.setText(desc);
        String pic = intent.getStringExtra("pic_url");
        Picasso.with(MoreNovelDetailsActivity.this).load(pic).into(icon);
        id = intent.getStringExtra("id");

        rl_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MoreNovelDetailsActivity.this, MoreNovelHeadActivity.class);
                intent.putExtra("desc", desc);
                startActivity(intent);
            }
        });
    }

    private void initEvent() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getDataFromWeb() {

        final String url = "http://lol.zhangyoubao.com/apis/rest/ItemsService/novels?catid=" +
                "10195&page=1&catwordid=" + id + "&i_=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&deviceId" +
                "=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&t_=1473044781108&p_=5669&v_=400901&a_=" +
                "lol&pkg_=com.anzogame.lol&d_=android&osv_=19&cha=360Martket&u_=15863305&modle_=MX4&api=" +
                "ItemsService.novels&time=1473044781&game=lol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA" +
                "&secretVersion=v1.0&nonce=711032&secretSignature=1nv+9SUC4xQ3QbRYtill3Aykk30=";

        new Thread(new Runnable() {
            @Override
            public void run() {

                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url).build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(MoreNovelDetailsActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String rusult = response.body().string();
                        Gson gson = new Gson();
                        mMoreNovelDetailsInfo = gson.fromJson(rusult, MoreNovelDetailsInfo.class);
                        if (mMoreNovelDetailsInfo.getData().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                });
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(MoreNovelDetailsActivity.this, "获取数据失败！", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    mMoreNovelDetailsAdapter = new MoreNovelDetailsAdapter(MoreNovelDetailsActivity.this, mMoreNovelDetailsInfo);
                    rl.setAdapter(mMoreNovelDetailsAdapter);
                    LinearLayoutManager llm = new LinearLayoutManager(MoreNovelDetailsActivity.this);
                    rl.setLayoutManager(llm);
                    break;
            }
        }
    };
}
